﻿Imports System.Data.OleDb

Public Class HotelDetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'If we arrive at this page from another page (not a resubmission)
        If IsPostBack = False Then
            'Check if query string of page has an ID parameter
            If Request.Params("ID") = "" Then Response.Redirect("HotelList.aspx")
            'Manually connect with Comments Database to show on the Second GridView
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TravelConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Comments WHERE Hotel_fkid = @f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            oleDbConn.Open()
            Dim dataReader = cmd.ExecuteReader()
            GridView2.DataSource = dataReader
            GridView2.DataBind()
        End If
    End Sub

    Protected Sub btn_AddComment_Click(sender As Object, e As EventArgs) Handles btn_AddComment.Click

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TravelConnectionString").ConnectionString)
        Dim SqlString As String = "INSERT INTO Comments(Author, Title, Comment, Hotel_fkid) VALUES (@f1, @f2, @f3, @f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_Author.Text)
        cmd.Parameters.AddWithValue("@f2", tb_Title.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Comment.Text)
        cmd.Parameters.AddWithValue("@f3", Request.Params("ID"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()

        'If we arrive here from the same page (page resubmission)
        Dim fillComments As String = "SELECT * FROM Comments WHERE Hotel_fkid = @f1"
        Dim fillCommetsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
        fillCommetsCmd.CommandType = CommandType.Text
        fillCommetsCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        Dim dataReader = fillCommetsCmd.ExecuteReader()
        GridView2.DataSource = dataReader
        GridView2.DataBind()
        clearComments()
    End Sub

    Private Function clearComments()
        tb_Author.Text = ""
        tb_Comment.Text = ""
        tb_Title.Text = ""
        Return Nothing
    End Function

    Protected Sub btn_Tag_Click(sender As Object, e As EventArgs) Handles btn_Tag.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TravelConnectionString").ConnectionString)
        Dim SqlString As String = "INSERT INTO HotelTagJunction(Hotel_fkid, Tags_fkid) VALUES (@f1, @f2)"
        oleDbConn.Open()

        For Each cb As ListItem In CheckBoxList1.Items
            If cb.Selected Then
                'Add join to Table (tags-hotels)
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If

        Next
        'check if text box is empty
        If tb_MultipleTags.Text = "" Then Return

        Dim newTags = tb_MultipleTags.Text.Split(",")
        For Each newTag In newTags
            'Add new tag to the Tags table
            Dim newTagSql = "INSERT INTO Tags(Tag) VALUES (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()

            'Find id of the new tag.
            Dim getLastIdCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 ID FROM Tags ORDER BY ID DESC", oleDbConn)
            Dim dr = getLastIdCmd.ExecuteReader()
            dr.Read()
            Dim lastId = dr.GetValue(0)

            'Assossiate this last id with the hotel id
            Dim newCmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            newCmd.CommandType = CommandType.Text
            newCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
            newCmd.Parameters.AddWithValue("@f2", lastId)
            newCmd.ExecuteNonQuery()
        Next
        CheckBoxList1.ClearSelection()
        tb_MultipleTags.Text = ""
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TravelConnectionString").ConnectionString)
        Dim SqlString As String = "INSERT INTO HotelTagJunction(Hotel_fkid, Tags_fkid) VALUES (@f1, @f2)"
        oleDbConn.Open()

        For Each lb As ListItem In ListBox1.Items
            If lb.Selected Then
                'Add join to Table (tags-hotels)
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
                cmd.Parameters.AddWithValue("@f2", lb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        
    End Sub
End Class